package at.ac.univie.zsu.aguataplan.ui;

import java.util.List;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import at.ac.univie.zsu.aguataplan.domain.DetectedEvent;
import at.ac.univie.zsu.aguataplan.domain.Game;
import at.ac.univie.zsu.aguataplan.domain.Video;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;
import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.property.I18nProperties;
import at.ac.univie.zsu.aguataplan.property.SystemProperties;
import at.ac.univie.zsu.aguataplan.service.GameManager;
import at.ac.univie.zsu.aguataplan.service.TeamManager;
import at.ac.univie.zsu.aguataplan.ui.template.IDialogList;
import at.ac.univie.zsu.aguataplan.ui.template.ListPanel;
import at.ac.univie.zsu.aguataplan.video.VideoService;

/**
 * @author gerry
 * 
 */
public class DialogAnalyze extends JDialog implements UIConstants {

	private static final long serialVersionUID = 1L;

	private MainWindow parent;
	private GeneralProperties generalProperties;
	private I18nProperties i18nProp;
	private SystemProperties systemProp;

	private TeamManager teamManager;
	private GameManager gameManager;
	private VideoService videoService;

	private DialogVideo dialogVideo;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public DialogAnalyze(MainWindow parent, GeneralProperties generalProperties) {
		super(parent, true);

		this.parent = parent;
		this.generalProperties = generalProperties;
		this.i18nProp = generalProperties.getI18nProperties();
		this.systemProp = generalProperties.getSystemProperties();
		this.teamManager = parent.getServices().getTeamManager();
		this.gameManager = parent.getServices().getGameManager();
		this.videoService = parent.getServices().getVideoService();

		this.setLayout(null);
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setLocationByPlatform(true);
		setSize(SPACING + PANEL_SIZE_X_LIST + SPACING + PANEL_SIZE_X_LIST + SPACING, SPACING + PANEL_SIZE_Y_LIST
				+ SPACING + SPACING + DIALOG_SIZE_Y_HEADER);
		setTitle(i18nProp.getString(ANALYZE_HEADER));
		setResizable(false);

		addEntries();

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	private void addEntries() {

		final ListPanel tournamentPanel = new ListPanel(this, generalProperties, new IDialogList() {

			@Override
			public List<?> initText() {
				List<Game> games = null;
				try {
					games = gameManager.getGameList();
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogAnalyze.this, i18nProp.getString(EX_GAME_LIST_DESC),
							i18nProp.getString(EX_GAME), JOptionPane.ERROR_MESSAGE);
				}
				return games;
			}

			@Override
			public void registerDoubleClick(Object obj) {
			}

			@Override
			public List<?> initCombo() {
				return null;
			}

			@Override
			public List<DetectedEvent> selectDestectedEvent(Object obj) {
				return null;
			}
		});
		tournamentPanel.init(i18nProp.getString(GAME_BORDER), PANEL_SIZE_X_LIST, PANEL_SIZE_Y_LIST, false);

		ListPanel videoPanel = new ListPanel(this, generalProperties, new IDialogList() {

			@Override
			public List<?> initText() {
				Game game = (Game) tournamentPanel.getSelectedValueEntry();

				List<Video> list = null;
				if (game == null) {
					return null;
				}
				try {
					list = gameManager.getGameVideoList(game);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogAnalyze.this, i18nProp.getString(EX_GAME_LIST_DESC),
							i18nProp.getString(EX_GAME), JOptionPane.ERROR_MESSAGE);

				}
				return list;
			}

			@Override
			public void registerDoubleClick(final Object obj) {

				Thread t = new Thread(new Runnable() {

					@Override
					public void run() {
						Video video = (Video) obj;
						System.out.println(video.toString());
						dialogVideo = new DialogVideo(DialogAnalyze.this, DialogAnalyze.this.parent,
								DialogAnalyze.this.generalProperties, video);
						dialogVideo.setVisible(true);

					}
				});
				t.start();

			}

			@Override
			public List<?> initCombo() {
				return null;
			}

			@Override
			public List<DetectedEvent> selectDestectedEvent(Object obj) {
				return null;
			}
		});
		videoPanel.init(i18nProp.getString(VIDEO_BORDER), PANEL_SIZE_X_LIST, PANEL_SIZE_Y_LIST, false);

		// set relationship
		tournamentPanel.addChild(videoPanel);
		videoPanel.addParent(tournamentPanel);

		// add to dialog
		tournamentPanel.setLocation(SPACING, SPACING);
		videoPanel.setLocation(SPACING + PANEL_SIZE_X_LIST, SPACING);
		this.add(tournamentPanel);
		this.add(videoPanel);

		// finish init
		tournamentPanel.finishInit();
		videoPanel.finishInit();

	}
}
